<template>
  <van-nav-bar
    title="创建订单"
    left-arrow
    fixed
    placeholder
    @click-left="onClickLeft"
  />
  <div class="co-header">
    <div class="coh-line">
      <div class="coh-line-p" :style="{ width: activeWidth }"></div>
    </div>
    <van-row class="coh-text" justify="space-around">
      <van-col
        v-for="item in progressInfo"
        :span="24 / progressInfo.length"
        :key="item.id"
        :style="{ opacity: active === item.id ? 1 : 0.7 }"
      >
        {{ item.name }}
      </van-col>
    </van-row>
  </div>
  <van-cell-group class="co-cell-box">
    <van-cell>
      <template #title>
        <div style="height: 100%; display: flex; align-items: center">
          <van-image
            width="20"
            style="margin-right: 6px"
            :src="createInfo.service.serviceImg"
          />
          <span class="custom-title">{{ createInfo.service.serviceName }}</span>
        </div>
      </template>
      <template #value>
        <div
          style="
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: end;
          "
        >
          <van-icon name="info-o" size="20px" style="margin-right: 6px" />
          <span>服务内容</span>
        </div>
      </template>
    </van-cell>
  </van-cell-group>
  <van-cell-group class="co-cell-box">
    <van-cell
      required
      title="医院选择"
      is-link
      :value="orderInfo.hospitalsName"
      @click="showPicker = true"
    />
    <van-field
      v-model="orderInfo.message"
      rows="2"
      autosize
      label="留言"
      type="textarea"
      placeholder="请输入留言"
      input-align="right"
    />
  </van-cell-group>
  <van-popup v-model:show="showPicker" destroy-on-close round position="bottom">
    <van-picker
      :model-value="pickerValue"
      :columns="hospitalsColumns"
      @cancel="showPicker = false"
      @confirm="onConfirm"
    />
  </van-popup>
</template>

<script setup>
import { ref, reactive, computed, onMounted, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance();
const router = useRouter();
const onClickLeft = () => {
  router.go(-1);
};
const active = ref(1);
const showPicker = ref(false);
const progressInfo = reactive([
  {
    id: 0,
    name: "填写订单",
  },
  {
    id: 1,
    name: "在线支付",
  },
  {
    id: 2,
    name: "专人服务",
  },
  {
    id: 3,
    name: "服务完成",
  },
]);
const pickerValue = ref(0);

const orderInfo = reactive({
  hospitalsName: "请选择医院",
  message: "",
});

const hospitalsColumns = ref([]);

const activeWidth = computed(() => {
  return ((active.value + 1) / progressInfo.length) * 100 + "%";
});

const createInfo = reactive({
  companion: [],
  hospitals: [],
  service: {},
});
const getCreateInfo = async () => {
  const res = await proxy.$api.getCreateInfo();
  if (res) {
    console.log(res);
    Object.assign(createInfo, res);
    hospitalsColumns.value = res.hospitals.map((e) => {
      e.text = e.name;
      e.value = e.id;
      return e;
    });

    console.log(hospitalsColumns.value);
  }
};

onMounted(() => {
  getCreateInfo();
});

const onConfirm = (e) => {
  console.log(e);
  orderInfo.hospitalsName = e.selectedOptions[0].name;
  showPicker.value = false;
};
</script>

<style lang="less" scoped>
.co-header {
  width: 100%;
  height: 100px;
  background: linear-gradient(to right, #0bb683, #0ca7ae);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
  .coh-line {
    width: 90%;
    height: 10px;
    border: 2.5px solid #fff;
    border-radius: 8px;
    background-color: #fff;
    display: flex;
    align-items: center;
    .coh-line-p {
      // width: 12%;
      height: 10px;
      border-radius: 5px;
      background-size: 100%;
      background: linear-gradient(to right, #0bb683, #0ca7ae);
    }
  }
  .coh-text {
    margin-top: 10px;
    width: 100%;
    text-align: center;
    color: #fff;
    font-size: 14px;
  }
}

.co-cell-box {
  width: 96%;
  margin-left: 2%;
  margin-bottom: 4px;
}
</style>
